Skip to content

Conversation

@reshma045
Copy link
Contributor

Resolves #8215

Changes:

This PR updates the p5.Vector.setHeading() method to support only 2D vectors in alignment with the proposed 2.x design.

  • Added a dimension check in setHeading():
  • If the vector’s z component is nonzero (i.e., not 2D), the method now emits a clear p5._friendlyError() explaining that setHeading() is 2D-only in p5.js 2.x.
  • For 2D vectors (z === 0), heading behavior remains the same, preserving magnitude and rotating in the xy-plane.
  • Keeps compatibility with existing sketches that use setHeading() on true 2D vectors.

PR Checklist

Copy link
Collaborator

@perminder-17 perminder-17 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really sorry for the delay in the review @reshma045 , I was out of my city.

Comment on lines +2203 to +2208
const m = this.mag();
if (m === 0) {
this.x = 0;
this.y = 0;
return this;
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this block of code is mostly redundant. When m === 0 we had this.x = m * Math.cos(a) which comes out to be this.x = 0 * Math.cos(a) which is this.x = 0, so we can remove this block and revert that change.

setHeading(a) {
if (this.isPInst) a = this._toRadians(a);
let m = this.mag();
if (this.z !== 0) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think, this condition is not correct, since the z component of the vector can be zero and it could still be a 3-d vector. Maybe you need to replace this.z !== 0 with this.z !== undefined.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, one more thought. Since setHeading() is logically meaningful only for 2d vectors, so do you think we need to check this.y === undefined as well?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants